home *** CD-ROM | disk | FTP | other *** search
- /*
- * Copyright 1995, Silicon Graphics, Inc.
- * All Rights Reserved.
- *
- * This is UNPUBLISHED PROPRIETARY SOURCE CODE of Silicon Graphics, Inc.;
- * the contents of this file may not be disclosed to third parties, copied or
- * duplicated in any form, in whole or in part, without the prior written
- * permission of Silicon Graphics, Inc.
- *
- * RESTRICTED RIGHTS LEGEND:
- * Use, duplication or disclosure by the Government is subject to restrictions
- * as set forth in subdivision (c)(1)(ii) of the Rights in Technical Data
- * and Computer Software clause at DFARS 252.227-7013, and/or in similar or
- * successor clauses in the FAR, DOD or NASA FAR Supplement. Unpublished -
- * rights reserved under the Copyright Laws of the United States.
- */
-
- /* histogram.c
- * A program to demonstrate the use of the histogram extension.
- * It reads an image and displays it in the left half of the window.
- * In the right half of the window the histogram is displayed for
- * the image.
- *
- * Escape key - exit the program
- */
- #include <GL/gl.h>
- #include <GL/glu.h>
- #include <GL/glut.h>
-
- #include <math.h>
- #include <stdio.h>
-
- #include "rgbImageFile.h" /* should be in ../../include */
-
- /* Function Prototypes */
-
- GLvoid initgfx( GLvoid );
- GLvoid drawScene( GLvoid );
- GLvoid reshape( GLsizei, GLsizei );
- GLvoid keyboard( GLubyte, GLint, GLint );
-
- void printHelp( char * );
-
- /* Global Definitions */
-
- #define KEY_ESC 27 /* ascii value for the escape key */
-
- /* Global Variables */
-
- static unsigned int *image;
- static int imgWidth, imgHeight;
- static GLsizei winWidth, winHeight;
-
- /* size of histogram table */
- static int histWidth = 256;
-
- GLvoid
- main( int argc, char *argv[] )
- {
- GLsizei width, height;
-
- glutInit( &argc, argv );
-
- if (argc < 2) {
- fprintf( stderr, "usage: %s <imageFileName>\n", argv[0] );
- exit (1);
- }
-
- image = rgbReadImageFile(argv[1], &imgWidth, &imgHeight);
- winWidth = imgWidth * 2;
- winHeight = imgHeight;
-
- glutInitWindowSize( 4, 4 );
- glutInitWindowSize( winWidth, winHeight );
- glutInitDisplayMode( GLUT_RGBA );
- glutCreateWindow( argv[0] );
-
- initgfx();
-
- glutKeyboardFunc( keyboard );
- glutReshapeFunc( reshape );
- glutDisplayFunc( drawScene );
-
- printHelp( argv[0] );
-
- glutMainLoop();
- }
-
- GLvoid
- printHelp( char *progname )
- {
- fprintf(stdout, "\n%s - use the hisgtogram extension to display an\n"
- "image with its histogram\n\n"
- "Escape key - exit the program\n",
- progname);
- }
-
- GLvoid
- initgfx( void )
- {
- if ( !glutExtensionSupported( "GL_EXT_histogram" ) ) {
- fprintf(stderr,
- "EXT_histogram not supported in this implementation\n");
- }
-
-
- #ifdef GL_EXT_histogram
- glEnable(GL_HISTOGRAM_EXT);
- glHistogramEXT(GL_HISTOGRAM_EXT,
- histWidth /* width */,
- GL_RGBA /* internalformat */,
- GL_FALSE /* sink */);
-
- glEnable(GL_MINMAX_EXT);
- glMinmaxEXT(GL_MINMAX_EXT,
- GL_RGBA /* internalformat */,
- GL_FALSE /* sink */);
- #endif
- }
-
- GLvoid
- keyboard( GLubyte key, GLint x, GLint y )
- {
- switch (key) {
- case KEY_ESC: /* Exit whenever the Escape key is pressed */
- exit(0);
- }
- }
-
- GLvoid
- reshape( GLsizei width, GLsizei height )
- {
- winWidth = width;
- winHeight = height;
-
- /* Create the first viewport - the left half of the window */
- glViewport( 0, 0, winWidth/2, winHeight );
-
- glMatrixMode( GL_PROJECTION );
- glLoadIdentity();
- gluOrtho2D( 0.0, (GLdouble) winWidth-1, 0.0, (GLdouble) winHeight-1 );
- glMatrixMode( GL_MODELVIEW );
- glLoadIdentity();
- glTranslatef( 0.375, 0.375, 0.0 );
- }
-
- static
- void displayHistogram( GLubyte *hgram, GLubyte *minmaxTable )
- {
- int i, j, channels = 3; /* RGB format */
-
- GLfloat colors[3][3];
- GLfloat min[3], max[3];
-
- colors[0][0] = 1.0; colors[0][1] = colors[0][2] = 0.0;
- colors[1][0] = 0.0; colors[1][1] = 1.0; colors[1][2] = 0.0;
- colors[2][0] = 0.0; colors[2][1] = 0.0; colors[2][2] = 1.0;
- min[0] = minmaxTable[0]; max[0] = minmaxTable[3];
- min[1] = minmaxTable[1]; max[1] = minmaxTable[4];
- min[2] = minmaxTable[2]; max[2] = minmaxTable[5];
-
- for (i = 0; i < channels; i++, hgram++) {
- GLubyte *next, *cur;
- GLfloat y, scale, height = imgHeight/channels;
-
- glColor3f(colors[i][0],colors[i][1],colors[i][2]);
-
- cur = hgram;
- next = cur + channels;
- for ( j = 0; j < histWidth; j++ ) {
- if ( *cur < *next ) { cur = next; }
- next += channels;
- }
- scale = (*cur == 0 ? scale = 0.0 : height / (GLfloat)(*cur));
-
- fprintf(stderr, "min[%d]=%f, max[%d]=%f, scale=%f\n",
- i, min[i], i, max[i], scale);
-
- glBegin(GL_LINE_STRIP);
- next = (GLubyte *)hgram;
- for (j = 0; j < histWidth; j++, next += channels) {
- y = *next * scale;
- glVertex2i(j, (GLuint) y);
- }
- glEnd();
- glTranslatef (0, height, 0.0);
- }
- }
-
- static GLubyte hgramTable[4096*4];
- static GLubyte minmaxTable[3*2*1];
-
- GLvoid
- drawScene( GLvoid )
- {
- /* Create the first viewport - the left half of the window */
- glViewport( 0, 0, winWidth/2, winHeight );
-
- glClear( GL_COLOR_BUFFER_BIT );
-
- glRasterPos2f( 0, 0 );
- glDrawPixels( imgWidth, imgHeight, GL_RGBA, GL_UNSIGNED_BYTE, image );
-
- #ifdef GL_EXT_histogram
- glGetHistogramParameterivEXT(GL_HISTOGRAM_EXT,
- GL_HISTOGRAM_WIDTH_EXT,
- &histWidth);
-
- glGetHistogramEXT(GL_HISTOGRAM_EXT,
- GL_FALSE, /* reset */
- GL_RGB, /* return format */
- GL_UNSIGNED_BYTE, /* return type */
- (GLvoid *)hgramTable);
-
- glGetMinmaxEXT(GL_MINMAX_EXT,
- GL_FALSE, /* reset */
- GL_RGB, /* return format */
- GL_UNSIGNED_BYTE, /* return type */
- (GLvoid *)minmaxTable);
- #endif
-
- /* Create the second viewport - right half of window */
- glViewport( winWidth/2 + 1, 0, winWidth/2, winHeight );
-
- displayHistogram( hgramTable, minmaxTable );
-
- glFlush();
-
- checkError("drawScene");
- }
-